package na.learn.java.threads;

import java.io.File;
import java.io.IOException;

public class FileManagerImpl implements FileManager {
	public static String EXTN = ".dll";

	public static void main(String[] args) {
		String path = "C:\\backup\\docs";
		/*if(args.length < 2){
			System.out.println("Usage:   java FileManagerImpl  <FUllPath>  <Extention>");
			return;
		}
		path = args[0];
		EXTN = args[1];
		
		System.out.println("You choose to rename file recursively at path " + path );
		try {
			int num = System.in.read();
			char c = (char)num;
			if(c != (char)'y' || c != (char)'Y')
				return;
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}*/
		
		
		new FileManagerImpl().recursivelyChangeExtention(new File(path));

	}

	public boolean recursivelyChangeExtention(File file) {

		if (file.isFile()) {

			String newFileName = file.getAbsolutePath().substring(
					0,
					file.getAbsolutePath().indexOf(".") > 0 ? file
							.getAbsolutePath().indexOf(".") : file
							.getAbsolutePath().length());
			file.renameTo(new File(newFileName + EXTN));
		} else if (file.isDirectory()) {

			for (File files : file.listFiles())
				recursivelyChangeExtention(files);
		}

		return true;
	}

	public boolean recursivelyAddExtention(File file) {

		if (file.isFile())
			file.renameTo(new File(file.getAbsolutePath() + EXTN));
		else if (file.isDirectory()) {

			for (File files : file.listFiles())
				recursivelyAddExtention(files);
		}

		return true;
	}

	public boolean recursivelyRemoveExtention(File file) {

		if (file.isFile()) {
			String newFileName = file.getAbsolutePath().substring(0,
					file.getAbsolutePath().length() - 3);
			file.renameTo(new File(newFileName));
		} else if (file.isDirectory()) {

			for (File files : file.listFiles())
				recursivelyRemoveExtention(files);
		}

		return true;
	}

}
